/*
 * @Author: richarfang
 * @Date: 2021-02-01 14:33:26
 * @LastEditTime: 2021-05-21 17:52:38
 * @LastEditors: richarfang
 * @Description: 神策埋点上报
 */

import {getCookie, setCookie} from "./cookie";

function handleGoodsType(goodsId) {
    if (goodsId) {
        return goodsId.slice(4, 7);
    }
    return '';
}

/**
 * 页面访问上报
 * 
 * ps:其他参数，根据产品数据需求自行补充
 */ 
export const sensorsViewPage = ({goods_id, spu_type, page_id, page_path, page_name, ...reportModule}) => {
    try {
        window.sensors.track('view_page', {
            page_id: page_id || window.page_id,
            page_path: page_path || window.page_path,
            page_name: page_name || window.page_name,
            app_id: window.APPID,
            l_program: 'H5',
            product_id: goods_id,
            product_type: spu_type || handleGoodsType(goods_id),
            ...reportModule
        })
    } catch (err) {
        console.log(err)
    }
}

/**
 * 页面点击上报
 * reportModule.page_button  {string}  页面id(必填)
 * 
 * ps:其他参数，根据产品数据需求自行补充
 */ 
export const sensorsTrackButton = ({goods_id, spu_type, page_id, page_path, page_name, ...reportModule}) => {
    try {
        window.sensors.track('click_butt', {
            page_id: page_id || window.page_id,
            page_path: page_path || window.page_path,
            page_name: page_name || window.page_name,
            product_id: goods_id,
            goods_id,
            product_type: spu_type || handleGoodsType(goods_id),
            ...reportModule
        })
    } catch (err) {
        console.log(err)
    }
}

export const sensorsTrackPerformance = (trackType, reportModule) => {
    try {
        window.sensors.track(trackType, {
            page_id: window.page_id,
            page_path: window.page_path,
            page_name: window.page_name,
            comm2: window._timeStamp,
            ...reportModule
        })
    } catch (err) {
        console.log(err)
    }
}

// 商详页和确认订单页共用的上报
export const wechatAdReport = (vm) => {
    let {landing_id, ad_type, gdt_vid} = vm.$route.query // 来自路由的

    // 来自cookie的
    let Clanding_id = getCookie('landing_id')
    let Cad_type = getCookie('ad_type')
    let Cgdt_vid = getCookie('gdt_vid')

    if(landing_id){
        setCookie('landing_id', landing_id, 1)
        setCookie('ad_type', ad_type, 1)
        setCookie('gdt_vid', gdt_vid, 1)

        sensorsViewPage({
            cmm3: landing_id,
            cmm4: ad_type,
            cmm5: gdt_vid
        })
    } else if(Clanding_id) {
        sensorsViewPage({
          cmm3: Clanding_id,
          cmm4: Cad_type,
          cmm5: Cgdt_vid
        })
    }
    // 原逻辑，供支付完成页使用
    sessionStorage.setItem('cmm3', landing_id || Clanding_id)
    sessionStorage.setItem('cmm4', ad_type || Cad_type)
    sessionStorage.setItem('cmm5', gdt_vid || Cgdt_vid)
}
